package com.prs.crm.action.hr;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;


import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;



import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.hr.Check;
import com.prs.crm.domain.hr.CheckPlan;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;
import com.prs.crm.service.hr.CheckPlanService;
import com.prs.crm.service.hr.CheckService;
import com.prs.crm.service.hr.PersonService;


import com.prs.crm.action.ReportAction;


public class CheckAction extends ReportAction{
	
	private static final long serialVersionUID = 4669677166507995583L;
	
	private Check check;
    private CheckService  checkService;
    private CheckPlan checkPlan;
    private CheckPlanService  checkPlanService;
  
    private List<CheckPlan> checkPlans;
    
	private PaginationSupport pagination;
	private String checkIds;
	private Date currentDate;
	private Person person;
	private List<Check> checkList;
    private List<Person> persons;
    private List<Check> checks;
	private  String workTime1;
	private  String restTime1;
	private List workTimeList;
	private PersonService personService;

	public Check getCheck() {
		return check;
	}


	public void setCheck(Check check) {
		this.check = check;
	}


	public CheckService getCheckService() {
		return checkService;
	}


	public void setCheckService(CheckService checkService) {
		this.checkService = checkService;
	}


	public PaginationSupport getPagination() {
		return pagination;
	}


	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public String getCheckIds() {
		return checkIds;
	}


	public void setCheckIds(String checkIds) {
		this.checkIds = checkIds;
	}


	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	
	@Action("editCheck1")
	public String editCheck1(){	
		person = AppContext.getAccountContext().currentUser();
		currentDate = new Date();
		return SUCCESS;
	}

	@Action("editCheck2")
	public String editCheck2(){	
		
		person = AppContext.getAccountContext().currentUser();
		currentDate = new Date(); 
		
		return SUCCESS;
		
	}
	@Action("editCurrentCheck1")
	public String editCurrentCheck1(){	
		
		persons=this.getPersonService().getAllPersons();
		currentDate = new Date(); 
		
		return SUCCESS;
	}
	@Action("editCurrentCheck2")
	public String editCurrentCheck2(){	
		
		
		currentDate = new Date(); 
		
		return SUCCESS;
	}
	@Clew(results={"success"})
	@Action(value="saveCheck2",results={@Result(name="success",type="redirect",location="editCheck2"),
			@Result(name="error",type="chain",location="editCheck2")})
	public String saveCheck2(){
		
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		currentDate = new Date(); 
		String currentDate1 =df.format(currentDate);
		Check check1= this.getCheckService().getCheckByname(check.getName(),check.getDate());
		//System.out.print("Object="+check1);
		if(check1==null||check1.equals("")){
			this.addActionError("请签到！");
			
			return ERROR;
		 }
		else{

		if(check1.getRestTime()==null||check1.getRestTime().equals("")){
			
			this.checkPlans=this.getCheckPlanService().getAllCheckPlan();
			
		    check1.setRestTime(currentDate1);
		    Date endTime;
		    Date currentDate11;
		   try{
				endTime = df.parse(checkPlans.get(0).getEndTime());
				//System.out.println(endTime);
				currentDate11=df.parse(currentDate1);
				if(currentDate11.getTime()<endTime.getTime()){
					 check1.setState2("0");
					 //早退
				}else if(currentDate11.getTime()>=endTime.getTime()){
					check1.setState2("1");
				}
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.getCheckService().saveOrUpdate(check1);
			this.setPrompt("提交成功");
			return SUCCESS;
		}else{
			this.addActionError("已签退");
			return ERROR;
		}
	  }
	}

	@Clew(results={"success"})
	@Action(value="saveCheck1",results={@Result(name="success",type="redirect",location="editCheck1"),
			@Result(name="error",type="redirect",location="editCheck1")})
	public String saveCheck1()
	{
	if (this.getCheckService().existSameCheck(check.getName(),check.getDate())) {
			this.addActionError("已签到");
			return ERROR;
		}
	 else{
		 SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		 this.checkPlans=this.getCheckPlanService().getAllCheckPlan();
		 String currentDate1 = df.format(new Date());
		 Date beginTime;
		 Date currentDate11;
		try {
			beginTime = df.parse(checkPlans.get(0).getBeginTime());
			currentDate11=df.parse(currentDate1);
			//System.out.println(beginTime);
			if(currentDate11.getTime()>beginTime.getTime()){
				 check.setState1("0");
                 //迟到
			 }
			else if(currentDate11.getTime()<=beginTime.getTime()){
				 check.setState1("1");
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		check.setState2("2");
		this.getCheckService().save(check);
		
		this.setPrompt("提交成功");
		return SUCCESS;
		
	}
	
}
	@Clew(results={"success"})
	@Action(value="saveCurrentCheck1",results={@Result(name="success",type="redirect",location="editCurrentCheck1"),
			@Result(name="error",type="chain",location="editCurrentCheck1")})
	public String saveCurrentCheck1()
	{
	if (this.getCheckService().existSameCheck(check.getName(),check.getDate())) {
			this.addActionError("已签到");
			return ERROR;
		}
	else{
		 SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		 this.checkPlans=this.getCheckPlanService().getAllCheckPlan();
		 String currentDate1 = df.format(new Date());
		 Date beginTime;
		 Date currentDate11;
		try {
			beginTime = df.parse(checkPlans.get(0).getBeginTime());
			currentDate11=df.parse(currentDate1);
			//System.out.println(beginTime);
			if(currentDate11.getTime()>beginTime.getTime()){
				 check.setState1("0");
                 //迟到
			 }
			else if(currentDate11.getTime()<=beginTime.getTime()){
				 check.setState1("1");
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		check.setState2("2");
		this.getCheckService().save(check);
		
		this.setPrompt("提交成功");
		return SUCCESS;
		
	}
	
}
	@Clew(results={"success"})
	@Action(value="saveCurrentCheck2",results={@Result(name="success",type="redirect",location="editCurrentCheck2"),
			@Result(name="error",type="chain",location="editCurrentCheck2")})
	public String saveCurrentCheck2(){
		
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		currentDate = new Date(); 
		String currentDate1 =df.format(currentDate);
		Check check1= this.getCheckService().getCheckByname(check.getName(),check.getDate());
		if(check1==null||check1.equals("")){
			this.addActionError("请签到！");
			
			return ERROR;
		 }
		else{

		if(check1.getRestTime()==null||check1.getRestTime().equals("")){
			
			this.checkPlans=this.getCheckPlanService().getAllCheckPlan();
			
		    check1.setRestTime(currentDate1);
		    Date endTime;
		    Date currentDate11;
		   try{
				endTime = df.parse(checkPlans.get(0).getEndTime());
				//System.out.println(endTime);
				currentDate11=df.parse(currentDate1);
				if(currentDate11.getTime()<endTime.getTime()){
					 check1.setState2("0");
					 //早退
				}else if(currentDate11.getTime()>=endTime.getTime()){
					check1.setState2("1");
				}
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.getCheckService().saveOrUpdate(check1);
			this.setPrompt("提交成功");
			return SUCCESS;
		}else{
			this.addActionError("已签退");
			return ERROR;
		}
	  }
	}

	@Action(value="exportCheckReport", results = { @Result(name = "success"
		,type="stream",params={"contentType","${contentType}","inputName","inputStream"
		,"bufferSize","1024","contentDisposition","filename=${outfileName}"}) })
	public String reportPerson()
	{
		ReportTable table = new ReportTable("考勤报表");
		table.addCell(new ReportCell("序号","id"));
		table.addCell(new ReportCell("姓名","name"));
		table.addCell(new ReportCell("日期","date"));
		table.addCell(new ReportCell("签到时间","workTime"));
		table.addCell(new ReportCell("状态", "state1", Constrains
				.getReportCheck2()));
		table.addCell(new ReportCell("签退时间","restTime"));
		table.addCell(new ReportCell("状态", "state2", Constrains
				.getReportCheck2()));
		//table.addCell(new ReportCell(""))
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Check.class);
		//this.setOutFilePath(this.getReportService().reportPDF(criteria, table, new ExportConfig()));
		this.setOutFilePath(this.getReportService().report(criteria, table, new ExportConfig(),this.getExportType()));
		return SUCCESS;
	}
	
	@Action("listCheck")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Check.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCheckPlanService().listByPage(criteria, pagination));
		return SUCCESS;
		
	}
	
	@Action("listCurrentCheck")
	public String listCurrentCheck() {
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		currentDate = new Date();
		String date1=df.format(currentDate);
		
		System.out.println(date1);
		
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Person.class);
		  pagination = this.getPaginationHelper().prepare(pagination);

		  this.setPagination(this.getCheckService().listByPage(criteria, pagination));
		  
		  
		 
		   persons=this.getPersonService().getAllPersons();
		    
		    for(int count=0;count<persons.size();count++){
			
			String name=persons.get(count).getResume().getName();

			checkList=this.getCheckService().getChecks(name, date1);
			
			//Check check1=this.getCheckService().getCheckByname(name, date1);
			
			for(int i=0;i<checkList.size();i++){
				if(checkList.get(i).getWorkTime()==null&& checkList.get(i).getRestTime()==null)
				{
					workTime1="";
					restTime1="";
					
				}
				else{
					
					workTime1=checkList.get(i).getWorkTime();
					restTime1=checkList.get(i).getRestTime();
				}
				
				workTimeList=new ArrayList();
				workTimeList.add(workTime1);
				
				System.out.println(workTime1+"CCCCC");
				System.out.println(restTime1+"SSSSS");
			}
			
			
		 }
		  return SUCCESS;
	}
	@Action("viewCheck")
	public String view() {
		return SUCCESS;
	}


	public Date getCurrentDate() {
		return currentDate;
	}


	public void setCurrentDate(Date currentDate) {
		this.currentDate = currentDate;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}


	public PersonService getPersonService() {
		return personService;
	}


	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public CheckPlan getCheckPlan() {
		return checkPlan;
	}


	public void setCheckPlan(CheckPlan checkPlan) {
		this.checkPlan = checkPlan;
	}


	public CheckPlanService getCheckPlanService() {
		return checkPlanService;
	}


	public void setCheckPlanService(CheckPlanService checkPlanService) {
		this.checkPlanService = checkPlanService;
	}


	public List<CheckPlan> getCheckPlans() {
		return checkPlans;
	}


	public void setCheckPlans(List<CheckPlan> checkPlans) {
		this.checkPlans = checkPlans;
	}


	public List<Check> getCheckList() {
		return checkList;
	}


	public void setCheckList(List<Check> checkList) {
		this.checkList = checkList;
	}


	public List<Person> getPersons() {
		return persons;
	}


	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}


	public List<Check> getChecks() {
		return checks;
	}


	public void setChecks(List<Check> checks) {
		this.checks = checks;
	}


	public String getWorkTime1() {
		return workTime1;
	}


	public void setWorkTime1(String workTime1) {
		this.workTime1 = workTime1;
	}


	public List getWorkTimeList() {
		return workTimeList;
	}


	public void setWorkTimeList(List workTimeList) {
		this.workTimeList = workTimeList;
	}


	public String getRestTime1() {
		return restTime1;
	}


	public void setRestTime1(String restTime1) {
		this.restTime1 = restTime1;
	}

}
